//package com.xiaobai.web.exception;
//
//import cn.hutool.core.util.StrUtil;
//import cn.hutool.http.HttpStatus;
//import com.xiaobai.common.utils.result.R;
//import com.xiaobai.common.utils.result.Result;
//import com.xiaobai.common.utils.result.ResultCode;
//import com.xiaobai.common.utils.result.ServiceException;
//import com.xiaobai.common.utils.tool.CommonConstants;
//import com.xiaobai.common.utils.tool.HttpResponseUtils;
//import com.xiaobai.common.utils.tool.StringUtil;
//import jakarta.servlet.http.HttpServletResponse;
//import jakarta.validation.ValidationException;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.http.converter.HttpMessageNotReadableException;
//import org.springframework.security.access.AccessDeniedException;
//import org.springframework.security.authentication.InsufficientAuthenticationException;
//import org.springframework.stereotype.Component;
//import org.springframework.validation.BindException;
//import org.springframework.validation.BindingResult;
//import org.springframework.validation.FieldError;
//import org.springframework.web.HttpRequestMethodNotSupportedException;
//import org.springframework.web.bind.MethodArgumentNotValidException;
//import org.springframework.web.bind.MissingServletRequestParameterException;
//import org.springframework.web.bind.annotation.ControllerAdvice;
//import org.springframework.web.bind.annotation.ExceptionHandler;
//import org.springframework.web.client.HttpClientErrorException;
//import org.springframework.web.multipart.MaxUploadSizeExceededException;
//
//import java.io.IOException;
//import java.sql.SQLException;
//import java.util.List;
//
///**
// * 异常通用处理
// * @author yanxudong
// * @date 2022-10-19
// */
//@Component
//@ControllerAdvice
//public class WebExceptionHandler {
//
//    private Logger log = LoggerFactory.getLogger(WebExceptionHandler.class);
//
//    @ExceptionHandler(ServiceException.class)
//    public void handleException(HttpServletResponse response, ServiceException e) throws IOException {
//        Result result = R.fail(e.getResultCode());
//        // 自定义返回提示
//        if (StrUtil.isNotBlank(e.getMessage())) {
//            result = R.fail(e.getResultCode(), e.getMessage());
//        }
//        response.setStatus(HttpStatus.HTTP_OK);
//        HttpResponseUtils.responseJson(result, response);
//    }
//
//    @ExceptionHandler(SQLException.class)
//    public void handleSqlException(HttpServletResponse response, ServiceException e) throws IOException {
//        log.error(CommonErrorCode.SQL_ERROR.getMsg(), e);
//        Result result = R.fail(CommonErrorCode.SQL_ERROR);
//        response.setStatus(HttpStatus.HTTP_OK);
//        HttpResponseUtils.responseJson(result, response);
//    }
//
//    @ExceptionHandler(Exception.class)
//    public void handleException(HttpServletResponse response, Exception e) throws Exception {
//        log.error(CommonErrorCode.INTERNAL_SERVER_ERROR.getMsg(), e);
//        Result result = R.fail(ResultCode.INTERNAL_SERVER_ERROR);
//        response.setStatus(HttpStatus.HTTP_OK);
//        HttpResponseUtils.responseJson(result, response);
//    }
//
//    @ExceptionHandler(MissingServletRequestParameterException.class)
//    public void handleMissParamException(HttpServletResponse response, MissingServletRequestParameterException e) throws Exception {
//        log.error(CommonErrorCode.ARGUMENTS_MISS_ERROR.getMsg(), e);
//        Result result = R.fail(CommonErrorCode.ARGUMENTS_MISS_ERROR);
//        response.setStatus(HttpStatus.HTTP_OK);
//        HttpResponseUtils.responseJson(result, response);
//    }
//
//    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
//    public void handleRequestException(HttpServletResponse response, HttpRequestMethodNotSupportedException e) throws Exception {
//        Result result = R.fail(CommonErrorCode.METHOD_NOT_SUPPORTED);
//        response.setStatus(HttpStatus.HTTP_OK);
//        HttpResponseUtils.responseJson(result, response);
//    }
//
//    @ExceptionHandler(HttpMessageNotReadableException.class)
//    public void authenticationException(HttpServletResponse response, Exception e) throws Exception {
//        Result result = R.fail(CommonErrorCode.ARGUMENTS_VALID_ERROR, e.getMessage());
//        response.setStatus(HttpStatus.HTTP_OK);
//        HttpResponseUtils.responseJson(result, response);
//    }
//
//    /**
//     * 此处拦截统一认证未登录异常，否则会被统一的Exception处理拦截而导致授权码模式时未登录不会跳转到登录页面
//     * @param e
//     * @throws Exception
//     */
//    @ExceptionHandler(InsufficientAuthenticationException.class)
//    public void handleException(InsufficientAuthenticationException e) {
//        throw e;
//    }
//
//    /**
//     * 参数校验异常
//     *
//     * @param response
//     * @param e
//     * @throws Exception
//     */
//    @ExceptionHandler(BindException.class)
//    public void bindException(HttpServletResponse response, BindException e) throws Exception {
//        Result result = R.fail(CommonErrorCode.INTERNAL_SERVER_ERROR);
//        String message = ExceptionUtil.getBindExceptionMessage(e);
//        if (StrUtil.isNotEmpty(message)) {
//            result = R.fail(message);
//        }
//        response.setStatus(HttpStatus.HTTP_OK);
//        HttpResponseUtils.responseJson(result, response);
//    }
//
//    /**
//     * JSR303参数校验异常处理
//     * @param e
//     * @return
//     */
//    @ExceptionHandler({MethodArgumentNotValidException.class})
//    public void argumentValidationException(HttpServletResponse response, MethodArgumentNotValidException e) throws IOException {
//        BindingResult bind = e.getBindingResult();
//        List<FieldError> errors = bind.getFieldErrors();
//        StringBuilder sb = new StringBuilder();
//        for (FieldError error : errors) {
//            sb.append(error.getField()).append("：").append(error.getDefaultMessage()).append("；");
//        }
//        response.setStatus(HttpStatus.HTTP_OK);
//        Result result = R.fail(CommonErrorCode.ARGUMENTS_VALID_ERROR.getCode(), sb.toString());
//        HttpResponseUtils.responseJson(result, response);
//    }
//
//    /**
//     * 参数校验异常
//     *
//     * @param response
//     * @param e
//     * @throws Exception
//     */
//    @ExceptionHandler(ValidationException.class)
//    public void violationException(HttpServletResponse response, ValidationException e) throws Exception {
//        String message = e.getMessage();
//        response.setStatus(HttpStatus.HTTP_OK);
//        if (message.contains(CommonConstants.SYMBOL_COLON)) {
//            message = message.split(CommonConstants.SYMBOL_COLON)[1].trim();
//        }
//        Result result = R.fail(CommonErrorCode.ARGUMENTS_VALID_ERROR.getCode(), message);
//        HttpResponseUtils.responseJson(result, response);
//    }
//
//    /**
//     * 认证异常
//     *
//     * @param response
//     * @param e
//     * @throws Exception
//     */
////    @ExceptionHandler(AuthException.class)
////    public void authException(HttpServletResponse response, AuthException e) throws Exception {
////        Result result = R.fail(e.getErrorCode());
////        // 自定义返回提示
////        if (StringUtil.isNotBlank(e.getMsg())) {
////            result = Result.error(e.getErrorCode(), e.getMsg());
////        }
////        response.setStatus(HttpStatus.HTTP_OK);
////        HttpResponseUtils.responseJson(result, response);
////    }
//
//    /**
//     * 无权访问接口
//     *
//     * @param response
//     * @param e
//     * @throws Exception
//     */
//    @ExceptionHandler(AccessDeniedException.class)
//    public void accessDeniedException(HttpServletResponse response, AccessDeniedException e) throws Exception {
//        log.error(AuthErrorCode.ACCESS_DENIED.getMsg(), e);
//        Result result = R.fail(AuthErrorCode.ACCESS_DENIED);
//        response.setStatus(HttpStatus.HTTP_OK);
//        HttpResponseUtils.responseJson(result, response);
//    }
//
//    @ExceptionHandler(MaxUploadSizeExceededException.class)
//    public void maxUploadSizeExceededException(HttpServletResponse response, MaxUploadSizeExceededException e) throws IOException {
//        log.error("上传文件超过限制", e);
//        Result res = R.fail(CommonErrorCode.MAX_UPLOAD_FILE_EXCEPTION, "上传文件超过最大文件限制");
//        HttpResponseUtils.responseJson(res, response);
//    }
//
//    @ExceptionHandler(IllegalStateException.class)
//    public void illegalStateException(HttpServletResponse response, IllegalStateException e) throws IOException {
//        log.error("上传文件超过限制", e);
//    }
//    /**
//     * HTTP访问异常
//     *
//     * @param response
//     * @param e
//     * @throws Exception
//     */
//    @ExceptionHandler(HttpClientErrorException.class)
//    public void httpException(HttpServletResponse response, HttpClientErrorException e) throws Exception {
//        log.error(AuthErrorCode.AUTHORIZED_ERROR.getMsg(), e);
//        String message = e.getMessage();
//        Result result = R.fail(CommonErrorCode.INTERNAL_SERVER_ERROR);
//        if (StringUtil.isNotBlank(message) && message.indexOf("error_description") > -1) {
//            message = message.substring(message.indexOf("error_description") + 17, message.length()).replace("\":\"", "").replace("\"}]", "");
//            result = R.fail(AuthErrorCode.AUTHORIZED_ERROR, message);
//        }
//        HttpResponseUtils.responseJson(result, response);
//    }
//
//}